#!/usr/bin/env python

# -------
# Imports:
# -----------
# Project configuration
from config import *

# ROS imports
import roslib; roslib.load_manifest(NODE_NAME)
import rospy

# Other
import threading 
from Queue import Empty

# ----------------------------------
class WorkerThread(threading.Thread):
# ----------------------------------
	# -------------------------------------------------
	def __init__(self, idNum, jobQueue, controller, do):
	# -------------------------------------------------
		# Super!
		threading.Thread.__init__(self)	

		# Store the paramaters
		self.idNum = idNum
		self.jobQueue = jobQueue
		self.controller = controller
		self.running = False
		self.do = do

	# -----------
	def run(self):
	# -----------
		# Run as long as the controller is running
		while self.controller.running:
			try:
				# Pop a job off the queue
				# A "job" is a tupple --> (particle, laser message)
				job = self.jobQueue.get(block=True, timeout = 0.001)

				# Note that we're running while we're processing a job
				self.running = True
	
				# Execute the job
				self.do(job)
				
				# Job's finished, not running again until we start another job
				self.running = False

			except Empty:
				continue
		
		# In case this thread shuts down as a result of stopping the controller
		self.running = False
